﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using CUREBusinessLogicLayer;
using System.Web.SessionState;
using System.Text.RegularExpressions;
using CUREDataAccessLayers;


namespace CURE.Handler
{
    /// <summary>
    /// UserHandler 的摘要说明
    /// </summary>
    public class UserHandler : IHttpHandler, IRequiresSessionState
    {
        public void ProcessRequest(HttpContext context)
        {
            MasterBll masterbll = new MasterBll();
            EventLogBll eventlogbll = new EventLogBll();
            string action = string.Empty;
            string master_id = string.Empty;
            string master_userid = string.Empty;
            string master_name = string.Empty;
            string master_sex = string.Empty;
            string master_dept = string.Empty;
            string master_email = string.Empty;
            string master_group = string.Empty;
            string master_tel = string.Empty;
            string master_job = string.Empty;
            string master_level = string.Empty;
            string master_transport = string.Empty;
            string master_packaging = string.Empty;
            string master_shipments = string.Empty;
            string isEMail = string.Empty;
            #region 处理参数
            action = context.Request.Form["action"];
            if (action == null || action == "")
            {
                action = context.Request.QueryString["action"];
            }
            master_id = context.Request.Form["txtid"];
            master_userid = context.Request.Form["txtEmployeeNum"];
            master_name = context.Request.Form["txtName"];
            master_sex = context.Request.Form["radSex"];
            master_email = context.Request.Form["txtEmail"];
            master_tel = context.Request.Form["txtTel"];
            master_group = context.Request.Form["txtGroup"];
            master_dept = context.Request.Form["txtDept"];
            master_job = context.Request.Form["SelectJob"];
            master_level = context.Request.Form["SelectSkilllever"];
            master_transport = context.Request.Form["txtTransport"];
            master_packaging = context.Request.Form["txtPackaging"];
            master_shipments = context.Request.Form["txtShipments"];
            isEMail = context.Request.Form["IsEmail"];

            string editid = context.Request.QueryString["editid"];
            string delId = context.Request.QueryString["deleteid"];
            string resid = context.Request.QueryString["restoreid"];
            #endregion
            Dictionary<string, object> dic = new Dictionary<string, object>();
            //dic.Add("oldid", editid);
            dic.Add("id", master_id);
            dic.Add("userid", master_userid);
            dic.Add("name", master_name);
            dic.Add("sex", master_sex);
            dic.Add("dept", master_dept);
            dic.Add("group", master_group);
            dic.Add("email", master_email);
            dic.Add("tel", master_tel);
            dic.Add("job", master_job);
            dic.Add("level", master_level);
            dic.Add("skill_transport", master_transport);
            dic.Add("skill_packaging", master_packaging);
            dic.Add("skill_shipments", master_shipments);
            dic.Add("isEMail", isEMail);

            string masterid = string.Empty;//当前登录用户
            if (HttpContext.Current.Request.Cookies["MWS_User"] != null)
            {
                masterid = HttpContext.Current.Request.Cookies["MWS_User"]["ID"];
            }
            string mastername = string.Empty;//当前登录用户
            if (HttpContext.Current.Request.Cookies["MWS_User"] != null)
            {
                mastername = HttpContext.Current.Request.Cookies["MWS_User"]["NAME"];
            }
            if (action == "add")
            {
                
                    bool resultadd = masterbll.InsertMasterData(dic);
                    if (resultadd == true)
                    {
                        //成功
                        context.Response.Write(success("SystemManagement/UserManagement.aspx"));
                        Dictionary<string, object> eventlogdic = new Dictionary<string, object>();
                        eventlogdic.Add("masterid", masterid);
                        eventlogdic.Add("msg", "用户管理操作：新增用户" + master_name + "，用户编号为" + master_userid + "。");
                        eventlogbll.InsertEventLog(eventlogdic);
                    }
                    else
                    {
                        //失败
                        context.Response.Write(failure("用户编号或者姓名已存在。"));
                    }
               
            }
            else if (action == "edit")
            {
                bool resultadd = masterbll.UpdateMaterData(dic);
                if (resultadd == true)
                {
                    //成功
                    context.Response.Write(success("SystemManagement/UserManagement.aspx"));
                    Dictionary<string, object> eventlogdic = new Dictionary<string, object>();
                    eventlogdic.Add("masterid", masterid);
                    eventlogdic.Add("msg", "用户管理操作：修改用户" + master_name + "，用户编号为" + master_userid + "。");
                    eventlogbll.InsertEventLog(eventlogdic);
                }
                else
                {
                    //失败
                    context.Response.Write(failure("用户编号已存在。"));
                }
            }
            else if (action == "delete")
            {
                bool resultadd = masterbll.DeleteMasterData(delId);
                if (resultadd == true)
                {
                    //成功
                    context.Response.Write(success1());
                    Dictionary<string, object> eventlogdic = new Dictionary<string, object>();
                    eventlogdic.Add("masterid", masterid);
                    eventlogdic.Add("msg", "用户管理操作：删除用户，ID号为" + delId + "。");
                    eventlogbll.InsertEventLog(eventlogdic);
                }
                else
                {
                    //失败
                    context.Response.Write(failure());
                }
            }
            else if (action == "restore")
            {
                bool resultres = masterbll.RestoreMaterData(resid);
                if (resultres == true)
                {
                    //成功
                    context.Response.Write(success1());
                    Dictionary<string, object> eventlogdic = new Dictionary<string, object>();
                    eventlogdic.Add("masterid", masterid);
                    eventlogdic.Add("msg", "用户管理操作：重置密码，ID号为" + resid + "。");
                    eventlogbll.InsertEventLog(eventlogdic);
                }
                else
                {
                    //失败
                    context.Response.Write(failure());
                }

            }
            else if (action == "changePassword")
            {
                Dictionary<string, object> dicpsw = new Dictionary<string, object>();
                string oldPass = context.Request.Form["txtOldPass"];//获得新密码
                string newPass = context.Request.Form["txtNewPass"];//获得新密码
                string arrirmPass = context.Request.Form["txtArrirmPass"];//获得确认密码

                oldPass = MasterDal.GetMD5Hash(oldPass);
                newPass = MasterDal.GetMD5Hash(newPass);
                arrirmPass = MasterDal.GetMD5Hash(arrirmPass);

                dicpsw.Add("userid", masterid);
                dicpsw.Add("password", newPass);
                dicpsw.Add("oldpassword", oldPass);
                if (masterbll.CheckMasterPassword(dicpsw))
                {
                    if (newPass.Equals(arrirmPass))
                    {
                        bool resultres = masterbll.UpdateMasterPassword(dicpsw);
                        if (resultres == true)
                        {
                            //成功
                            context.Response.Write(success());
                            Dictionary<string, object> eventlogdic = new Dictionary<string, object>();
                            eventlogdic.Add("masterid", masterid);
                            eventlogdic.Add("msg", "用户管理操作：修改密码，ID号为" + mastername + "。");
                            eventlogbll.InsertEventLog(eventlogdic);
                        }
                        else
                        {
                            //失败
                            context.Response.Write(failure());
                        }
                    }
                    else
                    {
                        context.Response.Write(failure("新密码与确认密码不一致！"));
                    }
                }
                else
                {
                    context.Response.Write(failure("初始密码不正确！"));
                }
            }
            else
            {
                context.Response.Write(failure("无指定操作"));
            }
        }

        /// <summary>
        /// 判断编号是否是数字
        /// </summary>
        /// <param name="dept_id"></param>
        /// <returns></returns>
        public bool JudgeIntOrNot(string id)
        {
            bool judge = false;
            try
            {
                int i = Convert.ToInt16(id);
                judge = true;
            }
            catch
            {
                judge = false;
            }
            return judge;
        }

        #region 提示信息
        /// <summary>
        /// 操作失败(无参数)
        /// </summary>
        /// <returns></returns>
        public string failure()
        {
            return "{\"statusCode\":\"300\", \"message\":\"操作失败\"}";
        }
        /// <summary>
        /// 操作失败(有参数)
        /// </summary>
        /// <param name="str">错误提示</param>
        /// <returns></returns>
        public string failure(string str)
        {
            return "{\"statusCode\":\"300\", \"message\":\"操作失败!" + str + "\"}";
        }
        /// <summary>
        /// 操作成功(无参数)
        /// </summary>
        /// <returns></returns>
        public string success()
        {
            return "{\"statusCode\":\"200\", \"message\":\"操作成功!\",\"callbackType\":\"closeCurrent\"}";
        }

        public string success1()
        {
            return "{\"statusCode\":\"200\", \"message\":\"操作成功!\"}";
        }

        /// <summary>
        /// 操作成功(有参数)
        /// </summary>
        /// <param name="str">信息</param>
        /// <param name="navTabId">页面指向</param>
        /// <returns></returns>
        public string success(string str, string navTabId)
        {
            return "{\"statusCode\":\"200\", \"message\":\"操作成功!" + str + "\",\"navTabId\":\"" + navTabId + "\"}";
        }
        public string success(string navTabId)
        {
            return "{\"statusCode\":\"200\", \"message\":\"操作成功!\",\"callbackType\":\"closeCurrent\",\"navTabId\":\"" + navTabId + "\"}";
        }
        #endregion

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}